package com.csjy.migration.hnnd;

import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.annotation.Dict;
import com.csjy.migration.hnnd.business.AbstractBusiness;
import com.csjy.migration.hnnd.business.SourceBusinessData;
import com.csjy.migration.hnnd.dict.AbstractDict;
import com.csjy.migration.hnnd.dict.FileType;

import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 字典工具类
 *  匹配原则
 */
public class DictTool {
  static Map<String, Map<String,String>> summaryDictMap = new HashMap<>();

  static {
    try {
      Set<Class<?>> set  = Tool.getAnnotationClasses("com.csjy.migration.hnnd.dict", Dict.class);
      for (Class<?> c : set) {
        Dict dictAnnotation = c.getAnnotation(Dict.class);
        Class<AbstractDict> x = (Class<AbstractDict>)c;
        AbstractDict abstractDict = x.newInstance();
        abstractDict.build();
        summaryDictMap.put(c.getSimpleName(),abstractDict.dictMap);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }


  /**
   * 获取目标字典编码
   * @param sourceDictCode
   * @return
   */
  public static String getTargetDictCode(Class<? extends AbstractDict> clazz,String sourceDictCode) {
    if(sourceDictCode == null || sourceDictCode.equals("")) {
      return null;
    }
    Map<String,String> singleDictMap = summaryDictMap.get(clazz.getSimpleName());
    String targetDictCode =  singleDictMap.get(sourceDictCode);
    if(targetDictCode == null) {
      return sourceDictCode;
    }
    return targetDictCode;
  }

  /**
   * 获取目标字典编码
   * @param sourceDictCode
   * @return
   */
  public static String getTargetDictCode(Class<? extends AbstractDict> clazz,Object sourceDictCode) {
    if(sourceDictCode == null) {
      return null;
    }
    return DictTool.getTargetDictCode(clazz,sourceDictCode.toString());

  }
}
